package nc.impl.hrppl.pjapplication;

import java.util.List;
import java.util.Map;

import nc.bs.dao.BaseDAO;
import nc.itf.hrppl.pjapplication.IQueryPJApplVOService;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapProcessor;
import nc.vo.pub.BusinessException;
import nccloud.framework.web.processor.reftree.TreeRefQueryInfo;

public class QueryPJApplVOServiceImpl implements IQueryPJApplVOService{
	private BaseDAO baseDAO;
	@Override
	public Integer queryPJApplVOLevelByPK(Object pk_pjappl) throws BusinessException {
		Integer level = 1;
		if(pk_pjappl == null) return level;
		String sql = this.builderHVOSQL(pk_pjappl);
		Object obj = this.getBaseDAO().executeQuery(sql, new ColumnProcessor());
		if(obj != null) {
			level += Integer.valueOf(obj.toString());
		}
		return level;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void setEPsPKs(TreeRefQueryInfo refQueryInfo) throws BusinessException {
		String appCode = refQueryInfo.getAppCode();
		if(!"".equals(appCode)) {
			String sql = this.buildEpsLevelSQL(appCode);
			Object obj = this.getBaseDAO().executeQuery(sql, new ColumnListProcessor());
			if(obj != null) {
				List<String> eps = (List<String>)obj;
				String[] seps = eps.toArray(new String[eps.size()]);
				refQueryInfo.setFilterPids(seps);
			}
		}
	}

	@SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> queryFourCGproject(Object fourpro) throws BusinessException {
		if(fourpro == null) return null;
		String sql = this.buildFourCGProject(fourpro);
		Object obj = this.getBaseDAO().executeQuery(sql, new MapProcessor());
		if(obj == null) return null;
		Map<String,Object > res =  (Map<String,Object>)obj;
		return res;
	}

	/**
	 * 构建四级采购项目sql
	 * @param fourpro 四级采购项目
	 * @return
	 * @throws BusinessException
	 */
	private String buildFourCGProject(Object fourpro) throws BusinessException{
        String sql = " select  " +
                "    * " +
                " from hrpbm_fourcgxm " +
                " where 1 = 1 " +
                "   and  dr = 0 " +
                "   and approvestatus = 1 " +
                "   and pk_fourcgxm = '" + fourpro + "'";
		return sql;
	}
	/**
	 * 构建级次SQL
	 * @param appCode
	 * @return
	 * @throws BusinessException
	 */
	private String buildEpsLevelSQL(String appCode) throws BusinessException{
		StringBuffer sql = new StringBuffer();
		sql.append(" select ");
		sql.append("    pk_eps   ");
		sql.append("  from bd_project ");
		sql.append("     where dr = 0 ");
		if("M0272004".equals(appCode)) {// 常规项目 默认显示一二级
			sql.append("  and def1 in(  '1' ) ");
		}else if("M0272006".equals(appCode)) { // 资本项目
			sql.append("  and def1 in(  '2', '3','4' ) ");
		}
		return sql.toString();
	}

	/**
	 * 构建项目级次查询
	 * @param pk_pjappl
	 * @return
	 * @throws BusinessException
	 */
	private String builderHVOSQL(Object pk_pjappl) throws BusinessException{
        return " select def1 from bd_project where dr = 0 and pk_project = '" + pk_pjappl + "' ";
	}

	public BaseDAO getBaseDAO() {
		return baseDAO == null ? new BaseDAO() : baseDAO;
	}

}
